﻿using Hjt212Parser;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataCollectionorTest.EF;

namespace DataCollectionorTest
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            textBoxQn.Text = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            comboBoxCn.SelectedIndex = 0;
            comboBoxSt.SelectedIndex = 0;
            var now = DateTime.Now;
            dateTimePickerBeginTime.Value=new DateTime(now.Year,now.Month,now.Day,now.Hour,0,0).AddDays(-1);
            dateTimePickerEndTime.Value = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0);
        }

        private void checkBoxQn_CheckedChanged(object sender, EventArgs e)
        {
            textBoxQn.Enabled = checkBoxQn.Checked;
        }



        private void buttonSend_Click(object sender, EventArgs e)
        {
            var vo = ValidInfo();
            if (!vo.IsValid)
            {
                return;
            }
            var cn = Parser.GetStringByKey("CN", ";CN=" + comboBoxCn.Text.Replace(" ", ";"));
            if (string.IsNullOrWhiteSpace(cn))
            {
                MessageBox.Show(@"CN设置不正确。");
                return;
            }
            var interval = 0;
            try
            {
                interval = int.Parse(textBoxInterval.Text);
                if (interval < 1)
                {
                    MessageBox.Show(@"发送间隔设置错误。");
                    return;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(@"发送间隔设置错误。");
                return;
            }

            var times = 0;
            try
            {
                times = int.Parse(textBoxTimes.Text);
                if (times < 1)
                {
                    MessageBox.Show(@"发送次数设置错误。");
                    return;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(@"发送次数设置错误。");
                return;
            }
            try
            {
                using (var ctx = new ModelGateway())
                {
                    for (int i = 0; i < times; i++)
                    {
                        var result = "QN=" + vo.Qn + ";ST=" + comboBoxSt.Text + ";CN=" + cn + ";PW=" + textBoxPw.Text + ";MN=" + vo.Mn
                                     + ";Flag=" + textBoxFlag.Text + ";CP=&&" + comboBoxCp.Text + "&&";

                        send_ctr_cmd cmd = new send_ctr_cmd();
                        cmd.cn = cn;
                        cmd.is_sent = false;
                        cmd.mn = vo.Mn;
                        cmd.qn = vo.Qn;
                        cmd.send_time = DateTime.Now;
                        cmd.sent_time = DateTime.Now;
                        cmd.send_string = Crc.WrapByCrc(result, "monitor");


                        ctx.send_ctr_cmd.Add(cmd);
                        ThreadUtil.Sleep(interval);
                    }
                    ctx.SaveChanges();
                    MessageBox.Show(@"下发完成.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private ValueObj ValidInfo()
        {
            var obj = new ValueObj();
            var qn = Parser.GetStringByKey("QN", "#QN=" + textBoxQn.Text + ";");
            if (string.IsNullOrWhiteSpace(qn) || qn.Length != 17)
            {
                MessageBox.Show(@"QN设置不正确,QN必须是17位，格式为yyyyMMddHHmmssfff。");
                obj.IsValid = false;
                return obj;
            }
            var mn = Parser.GetStringByKey("MN", ";MN=" + textBoxMn.Text + ";");
            if (string.IsNullOrWhiteSpace(mn) || !(mn.Length == 14 || mn.Length == 24))
            {
                MessageBox.Show(@"MN设置不正确,MN必须是14或24位。");
                obj.IsValid = false;
                return obj;
            }

            obj.Qn = qn;
            obj.Mn = mn;
            return obj;
        }
        private void buttonAnalysis_Click(object sender, EventArgs e)
        {
            var vo = ValidInfo();
            if (!vo.IsValid) return;
            var beginTime = dateTimePickerBeginTime.Value;
            var endTime = dateTimePickerEndTime.Value;
           
            if (endTime <= beginTime)
            {
                MessageBox.Show(@"开始时间不能大于等于接收时间。");
                return;
            }
            var intervalReal = 0;
            try
            {
                intervalReal = int.Parse(textBoxIntervalReal.Text);
                if (intervalReal < 1)
                {
                    MessageBox.Show(@"实时间隔设置错误。");
                    return;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(@"实时间隔设置错误。");
                return;
            }
            var intervalMinute = 0;
            try
            {
                intervalMinute = int.Parse(textBoxIntervalMinute.Text);
                if (intervalMinute < 1)
                {
                    MessageBox.Show(@"分钟间隔设置错误。");
                    return;
                }
            }
            catch (Exception)
            {
                MessageBox.Show(@"分钟间隔设置错误。");
                return;
            }
            var span = endTime- beginTime;
            try
            {
                using (var ctx = new ModelGateway())
                {
                    var beginTime2011 = beginTime.AddMinutes(-intervalReal);
                    var endTime2011=endTime.AddMinutes(-intervalReal);
                    var items = ctx.received_msg.Where(t => t.mn == vo.Mn && t.cn.Equals("2011") && t.datatime.HasValue && t.datatime.Value >= beginTime2011 && t.datatime.Value < endTime2011 && t.is_length_right).Select(t => t.datatime).Distinct();
                    textBox2011.Text = items.Count() + @"/" + (int)(span.TotalMinutes / intervalReal);

                    var beginTime2051 = beginTime.AddMinutes(-intervalMinute);
                    var endTime2051 = endTime.AddMinutes(-intervalMinute);
                    items = ctx.received_msg.Where(t => t.mn == vo.Mn && t.cn.Equals("2051") && t.datatime.HasValue && t.datatime.Value >= beginTime2051 && t.datatime.Value < endTime2051 && t.is_length_right).Select(t => t.datatime).Distinct();
                    textBox2051.Text = items.Count() + @"/" + (int)(span.TotalMinutes / intervalMinute);

                    var beginTime2061 = beginTime.AddHours(-1);
                    var endTime2061 = endTime.AddHours(-1);
                    items = ctx.received_msg.Where(t => t.mn == vo.Mn && t.cn.Equals("2061") && t.datatime.HasValue && t.datatime.Value >= beginTime2061 && t.datatime.Value < endTime2061 && t.is_length_right).Select(t => t.datatime).Distinct();
                    textBox2061.Text = items.Count() + @"/" + (int)span.TotalHours;

                    var beginTime2031 = beginTime.AddDays(-1);
                    var endTime2031 = endTime.AddDays(-1);
                    items = ctx.received_msg.Where(t => t.mn == vo.Mn && t.cn.Equals("2031") && t.datatime.HasValue && t.datatime.Value >= beginTime2031 && t.datatime.Value < endTime2031 && t.is_length_right).Select(t => t.datatime).Distinct();
                    textBox2031.Text = items.Count() + @"/" +((int)((endTime- beginTime).TotalDays*10+9))/10;
                    items = ctx.received_msg.Where(t => t.mn == vo.Mn && t.cn.Equals("0000") && t.datatime.HasValue && t.datatime.Value >= beginTime && t.datatime.Value < endTime && t.is_length_right).Select(t => t.datatime).Distinct();
                    textBoxOffline.Text = items.Count().ToString();
                    items = ctx.received_msg.Where(t => t.mn == vo.Mn && t.datatime.HasValue && t.datatime.Value >= beginTime && t.datatime.Value < endTime && !t.is_length_right).Select(t => t.datatime);
                    var all = ctx.received_msg.Where(t => t.mn == vo.Mn && t.datatime.HasValue && t.datatime.Value >= beginTime && t.datatime.Value < endTime).Select(t => t.datatime);
                    textBoxCmdInvalid.Text = items.Count() + @"/" + all.Count();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button9011Count_Click(object sender, EventArgs e)
        {
            var vo = ValidInfo();
            if (vo.IsValid)
            {
                try
                {
                    using (var ctx = new ModelGateway())
                    {
                        var count = ctx.received_msg.Count(t => t.mn == vo.Mn && t.qn == vo.Qn && t.cn.Equals("9011"));
                        textBox9011Count.Text = count.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void button9012Count_Click(object sender, EventArgs e)
        {
            var vo = ValidInfo();
            if (vo.IsValid)
            {
                try
                {
                    using (var ctx = new ModelGateway())
                    {
                        var count = ctx.received_msg.Count(t => t.mn == vo.Mn && t.qn == vo.Qn && t.cn.Equals("9012"));
                        textBox9012Count.Text = count.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
